package com.icongyou.enterprise.talent_pool.controller;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_pool.entity.dto.TalentDemandDTO;
import com.icongyou.enterprise.talent_pool.entity.vo.CoreCompetenceResultVO;
import com.icongyou.enterprise.talent_pool.entity.vo.TalentMatchResultVO;
import com.icongyou.enterprise.talent_pool.entity.vo.TalentSearchHistoryListVO;
import com.icongyou.enterprise.talent_pool.service.TalentPoolService;
import com.icongyou.enterprise.talent_pool.service.TalentSearchHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/talent-pool")
@RequiredArgsConstructor
public class TalentPoolController {

    private final TalentPoolService talentPoolService;
    private final TalentSearchHistoryService talentSearchHistoryService;

    /**
     * 人才搜索接口
     */
    @PostMapping("/search")
    public Result<TalentMatchResultVO> searchTalents(@RequestBody TalentDemandDTO demandDTO) {
        try {
            TalentMatchResultVO result = talentPoolService.matchStudentsByDemand(demandDTO);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("人才搜索失败: " + e.getMessage());
        }
    }

    /**
     * 查询核心竞争力label生成结果
     */
    @GetMapping("/core-competence/{searchId}")
    public Result<CoreCompetenceResultVO> getCoreCompetence(@PathVariable String searchId) {
        try {
            CoreCompetenceResultVO result = talentPoolService.getCoreCompetenceResult(searchId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询核心竞争力失败: " + e.getMessage());
        }
    }

    /**
     * 获取搜索历史记录 - 改成 query 参数
     */
    @GetMapping("/search-history")
    public Result<TalentSearchHistoryListVO> getSearchHistory(
            @RequestParam("userId") String userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            TalentSearchHistoryListVO result = talentSearchHistoryService.getSearchHistoryPage(userId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取搜索历史失败: " + e.getMessage());
        }
    }

    /**
     * 删除搜索历史记录 - 改成 query 参数
     */
    @DeleteMapping("/search-history/{historyId}")
    public Result<Void> deleteSearchHistory(
            @RequestParam("userId") String userId,  // 改成 query 参数
            @PathVariable String historyId) {
        try {
            boolean success = talentSearchHistoryService.deleteSearchHistory(userId, historyId);
            if (success) {
                return Result.success();
            } else {
                return Result.error(404, "搜索历史记录不存在");
            }
        } catch (Exception e) {
            return Result.error("删除搜索历史失败: " + e.getMessage());
        }
    }

    /**
     * 清空搜索历史记录 - 改成 query 参数
     */
    @DeleteMapping("/search-history")
    public Result<Void> clearSearchHistory(@RequestParam("userId") String userId) {  // 改成 query 参数
        try {
            boolean success = talentSearchHistoryService.clearSearchHistory(userId);
            return success ? Result.success() : Result.error("清空搜索历史失败");
        } catch (Exception e) {
            return Result.error("清空搜索历史失败: " + e.getMessage());
        }
    }
}